import Reader.Word_Reader as wordReader
import solve as sl

"""win"""
restest = open(r"E:\project_python\NewN\NLP\Reader\restest.txt","w+",encoding="UTF-8")
finishtestfile = open(r"E:\project_python\NewN\NLP\Reader\finishtestfile.txt","w+",encoding="UTF-8")

"""Linux"""
# restest = open(r"/root/NewN/NLP/Reader/restest.txt","w+",encoding="UTF-8")
# finishtestfile = open(r"/root/NewN/NLP/Reader/finishtestfile.txt","w+",encoding="UTF-8")
NumWordList = []
NumWordList.append(wordReader.onewordList)
NumWordList.append(wordReader.twowordList)
NumWordList.append(wordReader.threewordList)
NumWordList.append(wordReader.fourwordList)
NumWordList.append(wordReader.fivewordList)
NumWordList.append(wordReader.sixwordList)

#去重

# noReapeatsentences = list(set(sl.sentences))
noReapeatsentences = sl.sentences
# print("有重复的句子为"+ str(len(sl.sentences)))
# print("无重复的句子数为:"+str(len(noReapeatsentences)))

#拿到首单词
def getwordsFirstword(words):
    firstWord = words.split(" ")[0]
    return firstWord


"""
处理所有句子的函数 分句
"""
def solveSentences(AllList):
    """
    :param AllList:传进来一个[[word,sentence],[word,sentence],[word,sentence]....] 左边是word右边是 有word的sentence
    :return: 返回所有单词和分句[单词,分局]
    """
    WordDispersivesentens_List = []
    for oneList in AllList:
        ressection = []
        section = str(oneList[1]).split(" ")
        for item in section:
            if '.' in item or ',' in item or '!' in item or '?' in item or ':' in item:
                character = item[-1:]
                # print(character)
                item = item[:-1]
                ressection.append(item)
                ressection.append(character)
            else:
                ressection.append(item)
        WordSentence_group = [oneList[0],ressection]
        WordDispersivesentens_List.append(WordSentence_group)
    # print(len(dispersivesentens)) 1584
    # print(len(AllList)) 1584
    return WordDispersivesentens_List


"""
处理单个句子的函数
"""
s = """Objectives: The current Intensive Care Information Systems (IC-ISs) collet and store monitoring data in an automated way and can replace all paper forms by an electronic equivalent, resulting in a paperless ICU. Future development of IC-ISs will now have to focus on bedside clinical decision support. The current IC-ISs are data-driven systems, with a two-layer software architecture. This software architecture is hardly maintainable and probably not the most optimal architecture to make the transition towards future systems with decision support. The aim of this research was to address the design of an alternative software architecture based on new paradigms. Methods: State-of-the art component middleware and agent technology were deployed to design and implement a software architecture for ICU data flow management. Results: An advanced multilayer architecture for efficient data flow management in the ICU has been designed. The architecture is both generic and scalable, which means that it neither depends on a particular ICU not on the deployed monitoring devices. Automatic device detection and Graphical User Interface generation are taken into account. Furthermore, a demonstrator has been developed as a proof that the proposed conceptual software architecture is feasible in practice. The core of the new architecture consists of Bed Decision Agents (BDAs). The introduction of BDAs, who perform specific dedicated tasks, improves the adaptability and maintainability of the future very complex IC-ISs. Conclusions: A software architecture, based on component, middleware and agent technology, is feasible and offers important advantages over the currently used two-layer software architecture."""
def solveSen(sentence):
    """
    :param sentence: 传进来一个字符串
    :return: 一个字符串的List
    """
    ressection = []  #结果列表
    section = sentence.split(" ")
    for item in section:
        if '.' in item or ',' in item or '!' in item or '?' in item or ':' in item:
            character = item[-1:]
            # print(character)
            item = item[:-1]
            ressection.append(item)
            ressection.append(character)
        else:
            ressection.append(item)
    return ressection
# print("Agents" in solveSen(s))

#传进去段 返回段落分词 t是段落 words是词
def wordBIO():
    """
    处理BIO
    """
    #所有词对应的所有句子 找出有词的句子
    noRepeatList1 = []
    noRepeatList2 = []
    noRepeatList3 = []
    noRepeatList4 = []
    noRepeatList5 = []
    noRepeatList6 = []
    ResList = []
    for jword in wordReader.onewordList:
        for ksenten in noReapeatsentences:
            #这里ksenten需要抓换成list
            if jword in solveSen(ksenten):
                list = [jword,ksenten]
                noRepeatList1.append(list)
    for jword in wordReader.twowordList:
        for ksenten in noReapeatsentences:
            # if jword in solveSen(ksenten):
            if jword in ksenten:#这里改成了如果 str的jword  在str的ksenten里面那就算存在 然后返回一个分词形式的ksenten
                list = [jword,ksenten]
                noRepeatList2.append(list)
    for jword in wordReader.threewordList:
        for ksenten in noReapeatsentences:
            # if jword in solveSen(ksenten):
            if jword in ksenten:
                list = [jword,ksenten]
                noRepeatList3.append(list)
    for jword in wordReader.fourwordList:
        for ksenten in noReapeatsentences:
            # if jword in solveSen(ksenten):
            if jword in ksenten:
                list = [jword,ksenten]
                noRepeatList4.append(list)
    for jword in wordReader.fivewordList:
        for ksenten in noReapeatsentences:
            # if jword in solveSen(ksenten):
            if jword in ksenten:
                list = [jword,ksenten]
                noRepeatList5.append(list)
    for jword in wordReader.sixwordList:
        for ksenten in noReapeatsentences:
            # if jword in solveSen(ksenten):
            if jword in ksenten:
                list = [jword,ksenten]
                noRepeatList6.append(list)
    AllLength = len(noRepeatList1)+len(noRepeatList2)+len(noRepeatList3)+len(noRepeatList4)+len(noRepeatList5)+len(noRepeatList6)
    print(len(noRepeatList1))
    print("所有句子为" + str(AllLength))
    """
    处理句子
    所有需要标注的句子在
    noRepeatList1[n]
    所有对应的词在
    noRepeatList1[n]
    """

    one_word_Dsentence_List = solveSentences(noRepeatList1)
    """这里处理的是"""
    two_word_Dsentence_List = solveSentences(noRepeatList2) #[单词,分句 ]
    three_word_Dsentence_List = solveSentences(noRepeatList3)
    four_word_Dsentence_List = solveSentences(noRepeatList4)
    five_word_Dsentence_List = solveSentences(noRepeatList5)
    six_word_Dsentence_List = solveSentences(noRepeatList6)

    """
    根据index设置BIO
    """
    finishedGroup = []
    #一单词 word_Destence_List 是单个[词,分句]
    for word_Dsentence_List in one_word_Dsentence_List:
        if(word_Dsentence_List[0] in word_Dsentence_List[1]):
            realindex = word_Dsentence_List[1].index(str(word_Dsentence_List[0]))
            print(realindex)
            #填充BIOList
            o = 'O'
            length = len(word_Dsentence_List[1])
            oneBIOList = [o for i in range(length)]
            oneBIOList[realindex] = 'B'
            finishedGroup.append([word_Dsentence_List[1],oneBIOList])
            print(str(word_Dsentence_List[0]))
            print([word_Dsentence_List[1], oneBIOList])
    for word_Dsentence_List in two_word_Dsentence_List:   #[[单词,分句...],[],[],[],...]
        #测试分词
        print("词"+str(word_Dsentence_List[0]))
        print("分句"+str(word_Dsentence_List[1]))
        #拿到第一个单词的index
        if (str(word_Dsentence_List[0]).split(" ")[0] in word_Dsentence_List[1]):
            realindex = word_Dsentence_List[1].index(str(word_Dsentence_List[0]).split(" ")[0])
            print(realindex)
            #填充BIOList
            o = 'O'
            length = len(word_Dsentence_List[1])
            oneBIOList = [o for i in range(length)]
            oneBIOList[realindex] = 'B'
            for i in range(1,2):
                oneBIOList[realindex+i] = 'I'
            finishedGroup.append([word_Dsentence_List[1], oneBIOList])
            print(str(word_Dsentence_List[0]))
            print([word_Dsentence_List[1], oneBIOList])
    for word_Dsentence_List in three_word_Dsentence_List:
        #测试分词
        # print("词"+str(word_Dsentence_List[0]))
        # print("分句"+str(word_Dsentence_List[1]))
        #拿到单词的index
        if (str(word_Dsentence_List[0]).split(" ")[0] in word_Dsentence_List[1]):
            realindex = word_Dsentence_List[1].index(str(word_Dsentence_List[0]).split(" ")[0])
            print(realindex)
            #填充BIOList
            o = 'O'
            length = len(word_Dsentence_List[1])
            oneBIOList = [o for i in range(length)]
            oneBIOList[realindex] = 'B'
            for i in range(1,3):
                oneBIOList[realindex+i] = 'I'
            finishedGroup.append([word_Dsentence_List[1], oneBIOList])
            print(str(word_Dsentence_List[0]))
            print([word_Dsentence_List[1], oneBIOList])
    for word_Dsentence_List in four_word_Dsentence_List:
        #拿到单词的index
        if (str(word_Dsentence_List[0]).split(" ")[0] in word_Dsentence_List[1]):
            realindex = word_Dsentence_List[1].index(str(word_Dsentence_List[0]).split(" ")[0])
            print(realindex)
            #填充BIOList
            o = 'O'
            length = len(word_Dsentence_List[1])
            oneBIOList = [o for i in range(length)]
            oneBIOList[realindex] = 'B'
            for i in range(1,4):
                oneBIOList[realindex+i] = 'I'
            finishedGroup.append([word_Dsentence_List[1], oneBIOList])
            print(str(word_Dsentence_List[0]))
            print([word_Dsentence_List[1], oneBIOList])
    for word_Dsentence_List in five_word_Dsentence_List:
        #测试分词
        # print("词"+str(word_Dsentence_List[0]))
        # print("分句"+str(word_Dsentence_List[1]))
        #拿到单词的index
        if (str(word_Dsentence_List[0]).split(" ")[0] in word_Dsentence_List[1]):
            realindex = word_Dsentence_List[1].index(str(word_Dsentence_List[0]).split(" ")[0])
            print(realindex)
            #填充BIOList
            o = 'O'
            length = len(word_Dsentence_List[1])
            oneBIOList = [o for i in range(length)]
            oneBIOList[realindex] = 'B'
            for i in range(1,5):
                oneBIOList[realindex+i] = 'I'
            finishedGroup.append([word_Dsentence_List[1], oneBIOList])
            print([word_Dsentence_List[1], oneBIOList])
    for word_Dsentence_List in six_word_Dsentence_List:
        #测试分词
        # print("词"+str(word_Dsentence_List[0]))
        # print("分句"+str(word_Dsentence_List[1]))
        #拿到单词的index
        if (str(word_Dsentence_List[0]).split(" ")[0] in word_Dsentence_List[1]):
            realindex = word_Dsentence_List[1].index(str(word_Dsentence_List[0]).split(" ")[0])
            print(realindex)
            #填充BIOList
            o = 'O'
            length = len(word_Dsentence_List[1])
            oneBIOList = [o for i in range(length)]
            oneBIOList[realindex] = 'B'
            for i in range(1,6):
                oneBIOList[realindex+i] = 'I'
            finishedGroup.append([word_Dsentence_List[1], oneBIOList])
            print([word_Dsentence_List[1], oneBIOList])
    return finishedGroup
list = wordBIO()
# print(len(list))
# print(list,file=finishtestfile)



